package org.htmlparser.beans;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.PrintStream;
import java.io.Serializable;
import java.net.URLConnection;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.EncodingChangeException;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class FilterBean
  implements Serializable
{
  public static final String PROP_CONNECTION_PROPERTY = "connection";
  public static final String PROP_NODES_PROPERTY = "nodes";
  public static final String PROP_TEXT_PROPERTY = "text";
  public static final String PROP_URL_PROPERTY = "URL";
  protected NodeFilter[] mFilters;
  protected NodeList mNodes;
  protected Parser mParser;
  protected PropertyChangeSupport mPropertySupport;
  protected boolean mRecursive;

  public FilterBean()
  {
    this.mPropertySupport = new PropertyChangeSupport(this);
    this.mParser = new Parser();
    this.mFilters = null;
    this.mNodes = null;
    this.mRecursive = true;
  }

  public static void main(String[] paramArrayOfString)
  {
    if (paramArrayOfString.length <= 0)
      System.out.println("Usage: java -classpath htmlparser.jar org.htmlparser.beans.FilterBean <http://whatever_url> [node name]");
    while (true)
    {
      return;
      FilterBean localFilterBean = new FilterBean();
      if (1 < paramArrayOfString.length)
      {
        NodeFilter[] arrayOfNodeFilter = new NodeFilter[1];
        arrayOfNodeFilter[0] = new TagNameFilter(paramArrayOfString[1]);
        localFilterBean.setFilters(arrayOfNodeFilter);
      }
      localFilterBean.setURL(paramArrayOfString[0]);
      System.out.println(localFilterBean.getText());
    }
  }

  public void addPropertyChangeListener(PropertyChangeListener paramPropertyChangeListener)
  {
    this.mPropertySupport.addPropertyChangeListener(paramPropertyChangeListener);
  }

  protected NodeList applyFilters()
    throws ParserException
  {
    NodeList localNodeList = this.mParser.parse(null);
    NodeFilter[] arrayOfNodeFilter = getFilters();
    if (arrayOfNodeFilter != null)
      for (int i = 0; i < arrayOfNodeFilter.length; ++i)
        localNodeList = localNodeList.extractAllNodesThatMatch(arrayOfNodeFilter[i], this.mRecursive);
    return localNodeList;
  }

  public URLConnection getConnection()
  {
    URLConnection localURLConnection;
    if (this.mParser != null)
      localURLConnection = this.mParser.getConnection();
    while (true)
    {
      return localURLConnection;
      localURLConnection = null;
    }
  }

  public NodeFilter[] getFilters()
  {
    return this.mFilters;
  }

  public NodeList getNodes()
  {
    if (this.mNodes == null)
      setNodes();
    return this.mNodes;
  }

  public Parser getParser()
  {
    return this.mParser;
  }

  public boolean getRecursive()
  {
    return this.mRecursive;
  }

  public String getText()
  {
    String str;
    NodeList localNodeList = getNodes();
    if (localNodeList.size() != 0)
    {
      StringBean localStringBean = new StringBean();
      for (int i = 0; i < localNodeList.size(); ++i)
        localNodeList.elementAt(i).accept(localStringBean);
      str = localStringBean.getStrings();
    }
    while (true)
    {
      return str;
      str = "";
    }
  }

  public String getURL()
  {
    String str;
    if (this.mParser != null)
      str = this.mParser.getURL();
    while (true)
    {
      return str;
      str = null;
    }
  }

  public void removePropertyChangeListener(PropertyChangeListener paramPropertyChangeListener)
  {
    this.mPropertySupport.removePropertyChangeListener(paramPropertyChangeListener);
  }

  public void setConnection(URLConnection paramURLConnection)
  {
    String str = getURL();
    URLConnection localURLConnection = getConnection();
    if (((localURLConnection == null) && (paramURLConnection != null)) || ((localURLConnection != null) && (!(localURLConnection.equals(paramURLConnection)))))
      try
      {
        if (this.mParser == null)
        {
          this.mParser = new Parser(paramURLConnection);
          this.mPropertySupport.firePropertyChange("URL", str, getURL());
          this.mPropertySupport.firePropertyChange("connection", localURLConnection, this.mParser.getConnection());
          setNodes();
        }
        else
        {
          this.mParser.setConnection(paramURLConnection);
        }
      }
      catch (ParserException localParserException)
      {
        updateNodes(new NodeList());
      }
  }

  public void setFilters(NodeFilter[] paramArrayOfNodeFilter)
  {
    this.mFilters = paramArrayOfNodeFilter;
    if (getParser() != null)
    {
      getParser().reset();
      setNodes();
    }
  }

  protected void setNodes()
  {
    if (getURL() != null);
    try
    {
      updateNodes(applyFilters());
      return;
    }
    catch (EncodingChangeException localEncodingChangeException)
    {
    }
    catch (ParserException localParserException1)
    {
      try
      {
        this.mParser.reset();
        updateNodes(applyFilters());
      }
      catch (ParserException localParserException2)
      {
        while (true)
          updateNodes(new NodeList());
        localParserException1 = localParserException1;
        updateNodes(new NodeList());
      }
    }
  }

  public void setParser(Parser paramParser)
  {
    this.mParser = paramParser;
    if (getFilters() != null)
      setNodes();
  }

  public void setRecursive(boolean paramBoolean)
  {
    this.mRecursive = paramBoolean;
  }

  public void setURL(String paramString)
  {
    String str = getURL();
    URLConnection localURLConnection = getConnection();
    if (((str == null) && (paramString != null)) || ((str != null) && (!(str.equals(paramString)))))
      try
      {
        if (this.mParser == null)
        {
          this.mParser = new Parser(paramString);
          this.mPropertySupport.firePropertyChange("URL", str, getURL());
          this.mPropertySupport.firePropertyChange("connection", localURLConnection, this.mParser.getConnection());
          setNodes();
        }
        else
        {
          this.mParser.setURL(paramString);
        }
      }
      catch (ParserException localParserException)
      {
        updateNodes(new NodeList());
      }
  }

  protected void updateNodes(NodeList paramNodeList)
  {
    NodeList localNodeList;
    String str1;
    String str2;
    if ((this.mNodes == null) || (!(this.mNodes.equals(paramNodeList))))
    {
      localNodeList = this.mNodes;
      if (localNodeList == null)
        break label99;
      str1 = getText();
      if (str1 == null)
        str1 = "";
      this.mNodes = paramNodeList;
      if (this.mNodes == null)
        break label105;
      str2 = getText();
    }
    while (true)
    {
      while (true)
      {
        if (str2 == null)
          str2 = "";
        this.mPropertySupport.firePropertyChange("nodes", localNodeList, paramNodeList);
        if (!(str2.equals(str1)))
          this.mPropertySupport.firePropertyChange("text", str1, str2);
        return;
        label99: str1 = "";
      }
      label105: str2 = "";
    }
  }
}